home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- +
- + LEDA 2.1.1 11-15-1991
- +
- +
- + basic.h
- +
- +
- + Copyright (c) 1991 by Max-Planck-Institut fuer Informatik
- + Im Stadtwald, 6600 Saarbruecken, FRG
- + All rights reserved.
- +
- *******************************************************************************/
-
-
- #ifndef BASICH
- #define BASICH
-
- #include <stream.h>
- #include <stdlib.h>
-
- #ifndef __TURBOC__
-
- #include <generic.h>
- #include <values.h>
-
- #else
-
- // TC values.h:
-
- #define MAXINT 0x7FFF
- #define MAXLONG 0x7FFFFFFFL
- #define MAXFLOAT 3.37E+38
- #define MAXDOUBLE 1.797693E+308
-
- // generic.h:
-
- #define name2(z, y) __name2(z, y)
- #define __name2(z, y) z##y
- #define name3(z, y, x) __name3(z, y, x)
- #define __name3(z, y, x) z##y##x
- #define name4(z, y, x, w) __name4(z, y, x, w)
- #define __name4(z, y, x, w) z##y##x##w
-
- #define declare(z, y) name2(z, declare)(y)
- #define implement(z, y) name2(z, implement)(y)
- #define declare2(z, y, x) name2(z, declare2)(y, x)
- #define implement2(z, y, x) name2(z, implement2)(y, x)
-
- #endif /* __TURBOC__ */
-
-
- //------------------------------------------------------------------------------
- // Global Type Definitions
- //------------------------------------------------------------------------------
-
- typedef void* ent;
-
-
- typedef ent* ent_ptr;
-
- typedef int (*CMP_ENT)(ent&,ent&);
- typedef ent (*ENT_TO_ENT)(ent&);
- typedef int (*ENT_TO_INT)(ent&);
- typedef void (*ENT_TO_VOID)(ent&);
-
- typedef int (*ANY_TO_INT)(...);
- typedef ent (*ANY_TO_ENT)(...);
- typedef void (*ANY_TO_VOID)(...);
-
-
-
-
-
- //------------------------------------------------------------------------------
- // Macros
- //------------------------------------------------------------------------------
-
- #define Infinity MAXINT
-
- #define nil NULL
-
- #ifdef __STDC__
- #define STRINGIZE(x) #x
- #else
- #define STRINGIZE(x) "x"
- #endif
-
- #define name5(a,b,c,d,e) name2(name2(a,b),name3(c,d,e))
-
- #define declare1(a,t) name2(a,declare1)(t)
- #define declare3(a,t1,t2,t3) name2(a,declare3)(t1,t2,t3)
- #define declare4(a,t1,t2,t3,t4) name2(a,declare4)(t1,t2,t3,t4)
-
- #define COMPARE(t,T) name3(t,T,_cmp)
- #define APPLY(t,T) name3(t,T,_apply)
- #define ORD(t,T) name3(t,T,_ord)
-
- #define Main main(int argc, char** argv)
-
- #define newline (cout << "\n").flush()
-
- #define forever for(;;)
-
- #define loop(a,b,c) for (a=b;a<=c;a++)
-
- #define Max(a,b) ( (a>b) ? a : b )
-
- #define Min(a,b) ( (a>b) ? b : a )
-
- #define SWAP(a,b) if (a!=b) { *(int*)&a ^= int(b); \
- *(int*)&b ^= int(a); \
- *(int*)&a ^= int(b); }
-
-
- // iteration macros for lists and sets:
-
- #define forall(x,S) for((S).init_iterator(); (S).next_element(x); )
- #define forall_items(x,S) for(x = (S).first_item(); x; x = (S).next_item(x) )
-
-
- //------------------------------------------------------------------------------
- // compare, Print, Read, Init, Copy, Clear, Ent
- // for basic builtin types
- //------------------------------------------------------------------------------
-
- //overloaded compare: used to define linear orders in virtual cmp functions
-
- inline int compare(char& x, char& y) { return x-y; }
- inline int compare(int& x, int& y) { return x-y; }
- inline int compare(void* x, void* y) { return int(x)-int(y); }
-
- extern int compare(double&, double&);
-
- //overloaded Print: used to define virtual print functions
-
- inline void Print(char& x, ostream& out = cout) { out << x; }
- inline void Print(int& x, ostream& out = cout) { out << x; }
- inline void Print(void* x, ostream& out = cout) { out << "0x" << hex(int(x)); }
-
-
- //overloaded Read: used to define virtual read functions
-
-
- inline void Read(char& x, istream& in = cin) { in >> x; }
- inline void Read(int& x, istream& in = cin) { in >> x; }
- inline void Read(void*, istream&) {}
-
-
- //overloaded Init: used to define virtual initialization functions
-
- inline ent Init(char& x) { x=0; return ent(x); }
- inline ent Init(int& x) { x=0; return ent(x); }
- inline ent Init(void* x) { x=0; return ent(x); }
-
-
- //overloaded Clear: used to define virtual clear functions
-
- inline void Clear(char& x) { x=0; }
- inline void Clear(int& x) { x=0; }
- inline void Clear(void* x) { x=x; }
-
- //overloaded Copy: used to define virtual copy functions
-
- inline ent Copy(const char& x) { return ent(x); }
- inline ent Copy(const int& x) { return ent(x); }
- inline ent Copy(void* x) { return ent(x); }
-
-
- //overloaded Ent: used for converting pointer types to void*
-
- inline ent Ent(void* x) { return (void*)(x); }
- inline ent Ent(int x) { return (void*)(x); }
-
-
- //------------------------------------------------------------------------------
- // Memory Management
- //------------------------------------------------------------------------------
-
- struct memory_elem_type { memory_elem_type* next; };
-
- typedef memory_elem_type* memory_elem_ptr;
-
-
- extern const int memory_word_size; // size of word (in bytes) 4
- extern const int memory_block_bytes; // size of block (in bytes) 2^k - 4
- extern const int memory_block_words; // size of block (in words)
- extern const int memory_max_size; // maximal size of a structure (words)
-
- extern memory_elem_ptr memory_free_list[];
- extern long int memory_total_count[];
-
- extern memory_elem_ptr memory_block_list;
- extern int memory_block_count;
-
- extern void memory_free_blocks();
- extern memory_elem_ptr memory_allocate_block(int);
- extern memory_elem_ptr allocate_words(int size);
- extern memory_elem_ptr Allocate_words(int size);
- extern void deallocate_words(void* p, int size);
- extern void Deallocate_words(void* p, int size);
- extern void deallocate_list(void* head,void* tail, int size);
-
- extern void print_statistics();
-
- inline memory_elem_ptr allocate_bytes(int n)
- { int s = n/memory_word_size;
- if (n%memory_word_size) s++;
- return allocate_words(s);
- }
-
- inline void deallocate_bytes(void* p, int n)
- { int s = n/memory_word_size;
- if (n%memory_word_size) s++;
- deallocate_words(p,s);
- }
-
-
- #define OPERATOR_NEW(size)\
- void* operator new(size_t)\
- { memory_elem_ptr p = memory_free_list[size];\
- if (p==0) p = memory_allocate_block(size);\
- memory_free_list[size] = p->next;\
- return p;\
- }
-
- #define OPERATOR_DEL(size)\
- void operator delete(void* p)\
- { memory_elem_ptr(p)->next = memory_free_list[size];\
- memory_free_list[size] = memory_elem_ptr(p);\
- }
-
- //------------------------------------------------------------------------------
- // Basic Data Types
- //------------------------------------------------------------------------------
-
- enum rel_pos { before = 1, after = 0 };
-
- enum direction { forward = 0, backward = 1 };
-
- //------------------------------------------------------------------------------
- // boolean
- //------------------------------------------------------------------------------
-
-
- /*
- enum bool { false = 0, true = 1 };
- */
-
-
- typedef int bool;
-
- extern const bool true;
- extern const bool false;
-
-
-
- //------------------------------------------------------------------------------
- // real numbers
- //------------------------------------------------------------------------------
-
- class real{
-
- struct rrep {
-
- double d;
- int n; // number of references
-
- rrep(double x=0);
-
- OPERATOR_NEW(3)
- OPERATOR_DEL(3)
-
- };
-
- rrep* p;
-
- public:
-
- real() { p = new rrep; }
- real(const double d) { p = new rrep(d); }
- real(const int d) { p = new rrep(d); }
- real(const real& r) { p = r.p; p->n++; }
- real(ent x) { p=(rrep*)x; p->n++; }
- void clear() { if (--p->n == 0) delete p; }
-
- ~real() { clear(); }
-
-
- double operator~() const { return p->d; }
-
- double* dp() { return &(p->d); }
-
- int round() const { return (p->d>0) ? int(p->d+0.5) : int(p->d-0.5); }
- int trunc() const { return int(p->d); }
-
- operator double() const { return p->d; }
-
- real operator-() const { return -(p->d); }
-
- real& operator=(const double x);
- real& operator=(const real& x);
-
- real& operator+=(const real& r) { *this = p->d + r.p->d; return *this ; }
- real& operator+=(double d) { *this = p->d + d; return *this ; }
-
- real& operator-=(const real& r) { *this = p->d - r.p->d; return *this ; }
- real& operator-=(double d) { *this = p->d - d; return *this ; }
-
- real& operator*=(const real& r) { *this = p->d * r.p->d; return *this ; }
- real& operator*=(double d) { *this = p->d * d; return *this ; }
-
- real& operator/=(const real& r) { *this = p->d / r.p->d; return *this ; }
- real& operator/=(double d) { *this = p->d / d; return *this ; }
-
-
-
-
- friend ostream& operator<<(ostream&, const real&);
- friend istream& operator>>(istream&, real&);
-
- friend int compare(real&, real&);
-
- friend ent Init(real& x) { x.p->n++; return ent(x.p); }
- friend ent Copy(const real& x) { x.p->n++; return ent(x.p); }
- friend void Clear(real& x) { x.clear(); }
- friend void Print(real& x, ostream& out = cout) { out << x; }
- friend void Read(real& x, istream& in = cin) { in >> x; }
-
- friend ent Ent(const real& x) { return ent(x.p); }
-
- };
-
- //------------------------------------------------------------------------------
- // REAL(cmp): reals with user defined linear order cmp
- //------------------------------------------------------------------------------
-
- #define REAL(cmp) name2(real_,cmp)
-
- #define REALdeclare(cmp)\
- struct REAL(cmp) : public real \
- { REAL(cmp)(ent p) : real(p) {}\
- REAL(cmp)(real r ) : real(r) {}\
- REAL(cmp)(REAL(cmp)& r) : real(r) {}\
- REAL(cmp)() {}\
- ~REAL(cmp)() {}\
- };\
- \
- int compare(REAL(cmp)& x, REAL(cmp)& y) { return cmp(x,y); }
-
-
- //------------------------------------------------------------------------------
- // INT(cmp): int with user defined linear order cmp
- //------------------------------------------------------------------------------
-
- #define INT(cmp) name2(int_,cmp)
-
- #define INTdeclare(cmp)\
- struct INT(cmp)\
- { int p;\
- INT(cmp)() { p = 0; }\
- INT(cmp)(const int i) { p = i;}\
- INT(cmp)(ent x) { p = (int)x; }\
- operator int() { return p; }\
- \
- friend ent Init(INT(cmp)& x) { x.p = 0; return ent(x.p);}\
- friend ent Copy(const INT(cmp)& x) { return ent(x.p); }\
- friend void Clear(INT(cmp)& x) { x.p = 0; }\
- friend void Print(INT(cmp)& x, ostream& out = cout){ out << x.p; }\
- friend void Read(INT(cmp)& x, istream& in = cin) { in >> x.p; }\
- friend ent Ent(const INT(cmp)& x) { return ent(x.p); }\
- \
- };\
- \
- int compare(INT(cmp)& x, INT(cmp)& y) { return cmp(x,y); }
-
-
- //------------------------------------------------------------------------------
- // strings
- //------------------------------------------------------------------------------
-
- extern char* str_dup(const char*);
- extern char* str_cat(const char*,const char*);
-
- class string {
-
- struct srep {
-
- char* s; // pointer to c-string
- int n; // number of references
-
- srep(char* p) { s = p; n = 1; }
-
- OPERATOR_NEW(2)
- OPERATOR_DEL(2)
-
- };
-
- srep* p;
-
- public:
-
-
- string() { p = new srep(str_dup(""));}
- string(const string& x) { x.p->n++; p = x.p; }
- string(ent x) { p=(srep*)x; p->n++; }
- string(int ) { p = new srep(str_dup("")); }
-
- string(const char*, ...); // form-like constructor
-
- #if defined(__GNUG__) || defined(__TURBOC__)
-
- cfront: ambiguity string(const char*, ...) <--> string(const char*)
-
- string(const char* s) { PTR = new srep(s);}
-
- #endif
-
-
- void clear() { if (--p->n == 0) { delete p->s; delete p; } }
-
- ~string() { clear(); }
-
-
- char* operator~() const { return str_dup(p->s); }
- char* cstring() const { return p->s; }
-
- int length() const;
- string sub(int,int) const;
- int pos(string,int=0) const;
-
- string operator()(int i, int j) const { return sub(i,j); }
- string head(int i) const { return sub(0,i-1); }
- string tail(int i) const { return sub(length()-i,length()-1); }
-
- string insert(string, int=0) const;
- string insert(int, string) const;
-
- string replace(string, string, int=1) const;
- string replace(int, int, string) const;
-
- string del(string, int=1) const;
- string del(int, int) const;
-
- void read(istream&, char=' ');
- void read(char delim=' ') { read(cin,delim); }
-
- string replace_all(string s1, string s2) const { return replace(s1,s2,0); }
- string replace(int i, string s) const { return replace(i,i,s); }
-
- string del_all(string s) const { return del(s,0); }
- string del(int i) const { return del(i,i); }
-
- void read_line(istream& I = cin) { read(I,'\n'); }
-
- string format(string) const;
-
- string& operator=(const char*);
- string& operator=(const string&);
-
- char operator[](int) const;
- char& operator[](int);
-
- string operator+(const string& x) const;
- string operator+(const char* s1) const;
- string& operator+=(const char* s1);
- string& operator+=(const string& x);
-
-
- friend int operator==(const string& x, const char* s) ;
- friend int operator==(const string& x, const string& y) ;
- friend int operator!=(const string& x, const char* s) ;
- friend int operator!=(const string& x, const string& y) ;
- friend int operator<(const string& x, const string& y) ;
- friend int operator>(const string& x, const string& y) ;
- friend int operator<=(const string& x, const string& y) ;
- friend int operator>=(const string& x, const string& y) ;
-
-
- friend istream& operator>>(istream&, string&);
- friend ostream& operator<<(ostream&, const string&) ;
-
- friend int compare(string& x, string& y);
-
- friend ent Init(string& x) { x.p->n++; return ent(x.p); }
- friend ent Copy(const string& x) { x.p->n++; return ent(x.p); }
- friend void Clear(string& x) { x.clear(); }
- friend void Print(string& x, ostream& out = cout) { out << x; }
- friend void Read(string& x, istream& in = cin) { in >> x; }
-
- friend ent Ent(const string& x) { return ent(x.p); }
-
- };
-
- //------------------------------------------------------------------------------
- // STRING(cmp): string with user defined linear order cmp
- //------------------------------------------------------------------------------
-
- #define STRING(cmp) name2(string_,cmp)
-
- #define STRINGdeclare(cmp)\
- struct STRING(cmp) : public string \
- { STRING(cmp)(ent p) : string(p) {}\
- STRING(cmp)(string s) : string(s) {}\
- STRING(cmp)(STRING(cmp)& s) : string(s) {}\
- STRING(cmp)() {}\
- ~ STRING(cmp)() {}\
- };\
- \
- int compare(STRING(cmp)& x, STRING(cmp)& y) { return cmp(x,y); }
-
-
- //------------------------------------------------------------------------------
- // some useful functions
- //------------------------------------------------------------------------------
-
- typedef int (*SIG_PF) (...);
-
- extern SIG_PF signal(int, SIG_PF);
-
- extern int interrupt_handler(...);
-
- inline SIG_PF catch_interrupts(SIG_PF handler = interrupt_handler)
- { return signal(2,handler); }
-
-
- #ifdef __TURBOC__
- inline void srandom(int seed) { srand(seed); }
- inline int random() { return rand(); }
- #endif
-
-
- extern void init_random(int=0);
- extern double rrandom(); // real random number in ]0..1[
-
- inline unsigned random(unsigned a, unsigned b)
- { return a + unsigned(random())%(b-a+1); }
-
- inline int random(int a, int b) { return a + random()%(b-a+1); }
-
-
-
- extern double used_time();
- extern double used_time(real&);
- extern double used_time(double&);
- extern float used_time(float&);
- extern void print_time(string s);
-
- inline void print_time() { print_time(""); }
-
-
-
- //------------------------------------------------------------------------------
- // input/output
- //------------------------------------------------------------------------------
-
- // Problems with cfront's stdarg on SPARCs
-
- #define Va_Start save_registers(); va_start
-
- extern "C" void save_registers(); // cfront&sparc: saves registers on stack
- // others: do nothing
-
-
- // if not GNU replace form calls by string constructor
-
- #ifndef __GNUG__
- #define form ~string
- #endif
-
-
-
- class file_ostream : public ostream {
-
- filebuf fb;
-
- public:
-
- file_ostream() : ostream(&fb) {}
- file_ostream(string);
-
- bool open(string);
- void close();
-
- ~file_ostream() { close(); }
-
- };
-
-
- class file_istream : public istream {
-
- filebuf fb;
-
- public:
-
- file_istream() : istream(&fb) {}
- file_istream(string);
-
- bool open(string);
- void close();
-
- ~file_istream() { close(); }
-
- };
-
-
-
-
- struct string_istream : public istream {
-
- char buf[2048];
-
- public:
-
- string_istream(string);
- string_istream(char*);
- string_istream(int, char**);
-
- };
-
-
- class string_ostream : public ostream {
-
- char buf[2048];
-
- public:
-
- string_ostream();
- string str();
-
- };
-
-
-
- #ifndef __TURBOC__
-
- struct cmd_ostream : public ostream {
-
- #ifndef __GNUG__
- stdiobuf b;
- #endif
- cmd_ostream(string);
-
- };
-
-
- struct cmd_istream : public istream {
-
- #ifndef __GNUG__
- stdiobuf b;
- #endif
-
- cmd_istream(string);
-
- };
-
- #endif
-
-
-
-
- extern int Yes(string s);
- extern int Yes();
-
- extern int read_int(string s);
- extern int read_int();
-
- extern char read_char(string s);
- extern char read_char();
-
- extern double read_real(string s);
- extern double read_real();
-
- extern string read_string(string s);
- extern string read_string();
-
- extern string read_line(istream& s);
- extern string read_line();
-
-
- //------------------------------------------------------------------------------
- // Error Handling
- //------------------------------------------------------------------------------
-
- typedef void (*PEH)(int,char*); // Pointer to Error Handler
-
- extern PEH p_error_handler;
- extern PEH set_error_handler(PEH);
- extern void default_error_handler(int,char*);
-
-
- inline void error_handler(int i, char* s) { p_error_handler(i,s); }
- inline void error_handler(int i, string s) { p_error_handler(i,~s); }
-
-
- //------------------------------------------------------------------------------
- // LEDA INIT
- //------------------------------------------------------------------------------
-
- extern const char* leda_version_string;
-
- class LEDA_INIT {
-
- char* init_list;
-
- public:
-
- LEDA_INIT();
- ~LEDA_INIT();
-
- };
-
- extern LEDA_INIT LEDA;
-
- #endif BASICH
-